package com.ruoyi.api.yh.comm;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;

import com.ruoyi.api.yh.version.ApiVersion;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.UploadResultEntity;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.yh.api.R;
import com.ruoyi.common.config.ServerConfig;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName UploadImageApiController
 * @Deacription TODO
 * @Author Administrator
 * @Date 2021/3/12 17:33
 * @Version 1.0
 **/
@ApiSupport(order = 0)
@Api(tags="统一上传接口")
@RestController
@ApiVersion(1)
@RequestMapping("/{version}/app-api/upload")
public class UploadApiController {
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private RuoYiConfig ruoYiConfig;
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    public R<UploadResultEntity> fileUpload(@RequestParam(value = "file", required = true) MultipartFile multipartFile) {
        UploadResultEntity uploadResultEntity=new UploadResultEntity();
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, multipartFile);
            String url = ruoYiConfig.getImageUrl() + fileName;

            uploadResultEntity.setAbsoluteURL(url);
            uploadResultEntity.setFileName(fileName);
            uploadResultEntity.setRelativeURL(FileUtils.getName(fileName));
            R.ok(uploadResultEntity);
        }
        catch (Exception e)
        {
            return  R.ok(uploadResultEntity);
        }

        return  R.ok(uploadResultEntity);
    }
 /*   @ResponseBody
    @RequestMapping(value = "/fileUploadNoLogin", method = RequestMethod.POST)
    public R<UploadResultEntity> fileUploadNoLogin(@RequestParam(value = "file", required = true) MultipartFile multipartFile, @RequestParam(value = "neiro", required = true) String neiro) {
        if(!neiro.equals("dsafdsafsdafdasfdsa")){
            return R.fail("无效");
        }
        UploadResultEntity uploadResultEntity= minioUtil.uploadFile(multipartFile);

        return  R.ok(uploadResultEntity);
    }*/

}
